<template>
	<u-popup
		v-model="value"
		mode="bottom"
		:mask="true"
		:popup="false"
		:closeable="true"
		:safe-area-inset-bottom="true"
		close-icon-color="#ffffff"
		:z-index="uZIndex"
		:maskCloseAble="maskCloseAble"
		@close="close"
	>
		<u-tabs v-if="value" :list="genTabsList" :is-scroll="true" :current="tabsIndex" @change="tabsChange" ref="tabs"></u-tabs>
		<view class="area-box">
			<view class="u-flex" :class="{ change: isChange }">
				<view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group>
								<u-cell-item v-for="(item, index) in data" :title="item.name" :arrow="false" :index="index" :key="index" @click="provinceChange">
									<u-icon v-if="isChooseP && selectProv.name === item.name" slot="right-icon" size="34" name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view>
				<view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group v-if="isChooseP">
								<u-cell-item v-for="(item, index) in selectProv.subs" :title="item.name" :arrow="false" :index="index" :key="index" @click="cityChange">
									<u-icon v-if="isChooseC && selectCity.name === item.name" slot="right-icon" size="34" name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view>
				<view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group v-if="isChooseC">
								<u-cell-item v-for="(item, index) in selectCity.subs" :title="item.name" :arrow="false" :index="index" :key="index" @click="areaChange">
									<u-icon v-if="isChooseA && selectArea.name === item.name" slot="right-icon" size="34" name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view>
			</view>
		</view>
	</u-popup>
</template>

<script>
/**
 * select-city 省市区级联选择器
 * @property {String} z-index 弹出时的z-index值（默认1075）
 * @property {String} city-value 默认选中的地区, 以英文空格分开
 * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker（默认true）
 */
export default {
	name: 'm-select-city',
	props: {
		// 通过双向绑定控制组件的弹出与收起
		value: {
			type: Boolean,
			default: false
		},
		// 默认选中的地区,以英文空格分开
		cityValue: {
			type: String,
			default() {
				return '';
			}
		},
		// 是否允许通过点击遮罩关闭Picker
		maskCloseAble: {
			type: Boolean,
			default: true
		},
		// 弹出的z-index值
		zIndex: {
			type: [String, Number],
			default: 0
		}
	},
	data() {
		return {
			data: this.$u.cache.get('GoodsRegion', []),
			selectProv: {
				subs: []
			},
			selectCity: {
				subs: []
			},
			selectArea: {
				subs: []
			},
			isChooseP: false, //是否已经选择了省
			isChooseC: false, //是否已经选择了市
			isChooseA: false, //是否已经选择了区
			tabsIndex: 0
		};
	},
	mounted() {
		var that = this;
		this.$u.get('data/api.goods/getRegion').then(ret => {
			if (ret.code) {
				this.$u.cache.set('GoodsRegion', ret.data);
				this.data = ret.data;
				this.init();
			}
		});
	},
	computed: {
		isChange() {
			return this.tabsIndex > 1;
		},
		genTabsList() {
			let tabsList = [
				{
					name: '请选择'
				}
			];
			if (this.isChooseP) {
				tabsList[0]['name'] = this.selectProv.name;
				tabsList[1] = {
					name: '请选择'
				};
			}
			if (this.isChooseC) {
				tabsList[1]['name'] = this.selectCity.name;
				tabsList[2] = {
					name: '请选择'
				};
			}
			if (this.isChooseA) {
				tabsList[2]['name'] = this.selectArea.name;
			}
			return tabsList;
		},
		uZIndex() {
			return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
		}
	},
	methods: {
		init() {
			this.cityValue = this.$u.trim(this.cityValue);
			if (this.cityValue.length > 0) {
				let attrs = this.cityValue.split(' ');
				if (attrs[0]) this.setProvince(attrs[0]);
				if (attrs[1]) this.setCity(attrs[1]);
				if (attrs[2]) this.setArea(attrs[2]);
			}
		},
		close() {
			this.$emit('input', false);
		},
		tabsChange(index) {
			this.tabsIndex = index;
		},
		setProvince(name) {
			this.data.map((v, k) => {
				if (name === v.name) {
					this.provinceChange(k);
				}
			});
		},
		setCity(name) {
			this.selectProv.subs.map((v, k) => {
				if (v.name === name) {
					this.cityChange(k);
				}
			});
		},
		setArea(name) {
			this.selectCity.subs.map((v, k) => {
				if (v.name === name) {
					this.areaChange(k);
				}
			});
		},
		provinceChange(index) {
			this.isChooseP = true;
			this.isChooseC = false;
			this.isChooseA = false;
			this.selectProv = this.data[index];
			this.tabsIndex = 1;
		},
		cityChange(index) {
			this.isChooseC = true;
			this.isChooseA = false;
			this.selectCity = this.selectProv.subs[index];
			this.tabsIndex = 2;
		},
		areaChange(index) {
			this.isChooseA = true;
			this.selectArea = this.selectCity.subs[index];
			this.$emit('city-change', {
				province: this.selectProv.name,
				city: this.selectCity.name,
				area: this.selectArea.name
			});
			this.close();
		}
	}
};
</script>

<style lang="scss">
.area-box {
	width: 100%;
	height: 800rpx;
	overflow: hidden;

	> view {
		width: 150%;
		transition: transform 0.3s ease-in-out 0s;
		transform: translateX(0);

		&.change {
			transform: translateX(-33.3333333%);
		}
	}

	.area-item {
		width: 33.3333333%;
		height: 800rpx;
	}
}
</style>
